//
//  CustomTabBarCtrl.m
//
//  Created by Northwan on 15-4-12.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "CustomTabBarCtrl.h"
#import "HomeCtrl.h"
#import "MsgCtrl.h"
#import "ServiceCtrl.h"
#import "PlanCtrl.h"
#import "MineCtrl.h"

@interface CustomTabBarCtrl ()<UITabBarControllerDelegate>

@property (nonatomic, weak) UIViewController *lastSelectedViewContoller;
@property (nonatomic, weak) HomeCtrl *home;
@property (nonatomic, weak) MsgCtrl *msg;
@property (nonatomic, weak) ServiceCtrl *service;
@property (nonatomic, weak) PlanCtrl *plan;
@property (nonatomic, weak) MineCtrl *mine;

@end

@implementation CustomTabBarCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDelegate:self];
    [self.tabBar setBackgroundColor:[UIColor whiteColor]];
    //首页
    HomeCtrl *home = [[HomeCtrl alloc]init];
    [self addOneChlildVc:home title:@"首页" imageName:@"tabar_home_icon" selectedImageName:@"tabar_home_icon"];
    _home = home;
    
    //资讯
    MsgCtrl *msg = [[MsgCtrl alloc]init];
    [self addOneChlildVc:msg title:@"资讯" imageName:@"tabar_msg_icon" selectedImageName:@"tabar_msg_icon"];
    _msg = msg;
    
    //旅游服务
    ServiceCtrl *service = [[ServiceCtrl alloc]init];
    [self addOneChlildVc:service title:@"旅游服务" imageName:@"tabar_service_icon" selectedImageName:@"tabar_service_icon"];
    _service = service;
    
    //行程规划
    PlanCtrl *plan = [[PlanCtrl alloc]init];
    [self addOneChlildVc:plan title:@"行程规划" imageName:@"tabar_plan_icon" selectedImageName:@"tabar_plan_icon"];
    _plan = plan;
    
    //我
    MineCtrl *mine = [[MineCtrl alloc]init];
    [self addOneChlildVc:mine title:@"我的" imageName:@"tabar_mine_icon" selectedImageName:@"tabar_mine_icon"];
    _mine = mine;
    
}



-(void)setSelectedClass:(Class)class{
    for (UINavigationController *ctrl in self.viewControllers) {
        if ([[ctrl.viewControllers firstObject] isKindOfClass:class]) {
            NSUInteger index = [self.viewControllers indexOfObject:ctrl];
            [self setSelectedIndex:index];
            break;
        }
    }
}

-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UINavigationController *)viewController{
    UIViewController *vc = [viewController.viewControllers firstObject];
    if ([vc isKindOfClass:[ServiceCtrl class]]) {
        if (self.lastSelectedViewContoller == vc) {
            
        }
    }
    self.lastSelectedViewContoller = vc;
}



/**
 *  添加一个子控制器
 *
 *  @param childVc           子控制器对象
 *  @param title             标题
 *  @param imageName         图标
 *  @param selectedImageName 选中的图标
 */
- (void)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    // 设置标题
    // 相当于同时设置了tabBarItem.title和navigationItem.title
    childVc.title = title;
    
    // 设置图标
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    
    // 设置选中的图标
    UIImage *selectedImage = [UIImage imageNamed:selectedImageName];
    // 声明这张图片用原图(别渲染)
    selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.tabBarItem.selectedImage = selectedImage;
    childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(2.5, 0, -2.5, 0);
    [childVc.tabBarItem setSelectedImage:selectedImage];
    // 设置tabBarItem的普通文字颜色
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childVc.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    // 设置tabBarItem的选中文字颜色
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    //此主题颜色需要修改TODO
    selectedTextAttrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    [childVc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    
    // 添加为tabbar控制器的子控制器
    UINavigationController *nav = [[CustomNavigationCtrl alloc] initWithRootViewController:childVc];
    [self addChildViewController:nav];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
